const express=require('express');
const pool=require('../pool.js');
const r=express.Router();
//后台登录
r.post("/product_login",(req,res)=>{
	if(req.body.uname=="root" && req.body.upwd=="123456"){
			res.send("1");
		}else{
			res.send("0");
		}
	})
	
//商品列表
r.get("/product_list",(req,res)=>{
	var sql="select * from product";
	pool.query(sql,(err,result)=>{
		if(err) throw err;
		res.send(result);
	})
})

//商品删除
r.delete("/product_delete/:pid",(req,res)=>{
	var _pid=req.params.pid;
	var sql="delete from product where pid=?";
	pool.query(sql,[_pid],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows>0){
			res.send("1");
		}else{
			res.send("0");
		}
	});
});

//商品查询
r.get("/product_search/:pid",(req,res)=>{
	var _pid=req.params.pid;
	var sql="select * from product where pid=?";
	pool.query(sql,[_pid],(err,result)=>{
		if(err) throw err;
		if(result.length>0){
			res.send(result);
		}else{
			res.send("0");
		}
	});
});

//商品修改
r.put("/product_update",(req,res)=>{
	var obj=req.body;
	var sql="update product set ? where pid=?";
	pool.query(sql,[obj,obj.pid],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows>0){
			res.send("1");
		}else{
			res.send("0");
		}
	})
})
//商品添加
r.post('/product_add',(req,res)=>{
    let obj=req.body;
    pool.query('insert into product set ?', [obj], (err, result) => {
        if (err) throw err;
        if(result.affectedRows>0){
        	res.send("1");
        }else{
        	res.send("0");
				}
    });
});
module.exports=r;